package com.ruoyi.framework.report.service;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.report.config.ReportConfig;
import com.ruoyi.framework.web.service.TokenService;
import org.jeecg.modules.jmreport.api.JmReportTokenServiceI;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;


import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Component
public class ReportTokenService implements JmReportTokenServiceI {

    // 若依框架token
    //@Value("${token.header}")
    //private String ryHeader;

    // 积木报表token
    private String jmHeader = "X-Access-Token";

    @Autowired
    private ReportConfig reportConfig;

    @Autowired
    private TokenService tokenService;


    @Override
    public String getUsername(String s) {
        LoginUser loginUser = tokenService.getLoginUser(s);
        return loginUser.getUsername();
    }

    @Override
    public String[] getRoles(String s) {
        LoginUser loginUser = tokenService.getLoginUser(s);
        SysUser user = loginUser.getUser();
        List<SysRole> roles = user.getRoles();
        String[] roleNameArray = roles.stream().map(SysRole::getRoleName).toArray(String[]::new);
        return roleNameArray;
    }

    @Override
    public Boolean verifyToken(String s) {
        LoginUser loginUser = tokenService.getLoginUser(s);
        if (StringUtils.isNotNull(loginUser)){
            tokenService.refreshToken(loginUser);
            SysUser user = loginUser.getUser();

            // 超级管理员放权
            if (StringUtils.isNotNull(user) && user.isAdmin()) {
                return true;
            } else {
                Set<String> permissions = loginUser.getPermissions();
                if (StringUtils.isNotNull(permissions) && (permissions.contains(reportConfig.getReportQueryPermission()) || permissions.contains(reportConfig.getReportEditPermission()) || permissions.contains(reportConfig.getDragQueryPermission()) || permissions.contains(reportConfig.getDragEditPermission()))) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public String getToken(HttpServletRequest request) {
        String token = request.getParameter("token");
        if (StringUtils.isNull(token)) {
            token = request.getHeader(jmHeader);
        }
        if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX))
        {
            token = token.replace(Constants.TOKEN_PREFIX, "");
        }
        return token;
    }

    @Override
    public Map<String, Object> getUserInfo(String token) {
        token = token.replace(Constants.TOKEN_PREFIX, "");
        LoginUser loginUser = tokenService.getLoginUser(token);
        Map<String, Object> map = new HashMap<>();
        map.put(SYS_USER_CODE, loginUser.getUserId());
        map.put(SYS_ORG_CODE, loginUser.getDeptId());
        return map;
    }

    @Override
    public HttpHeaders customApiHeader() {
        HttpHeaders headers = new HttpHeaders();
        //headers.add(ryHeader, Constants.TOKEN_PREFIX + getToken());
        headers.add(jmHeader, getToken());
        return headers;
    }
}
